package com.hanlin.backend.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hanlin.backend.common.dto.EnumDto;
import com.hanlin.backend.common.entity.HlEnum;
import com.hanlin.backend.common.service.HlEnumService;
import com.hanlin.backend.common.util.ResultCommon;
import com.hanlin.backend.common.util.TableCommon;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 枚举表 前端控制器
 * </p>
 *
 * @author hl.yuan
 * @since 2021-01-06
 */
@RestController
@Slf4j
@Api(value = "/frontend/api/enum", tags = "枚举")
public class HlEnumController {

    @Autowired
    private HlEnumService hlEnumService;

    @GetMapping("/frontend/api/enum/tableList")
    @ApiOperation("枚举列表")
    public ResultCommon<TableCommon<HlEnum>> tableList(EnumDto enumDto) {
        IPage<HlEnum> hlEnumList = hlEnumService.getTableList(enumDto);
        return enumDto.build(hlEnumList);
    }

    @GetMapping("/frontend/api/enum/getEnum")
    @ApiOperation("获取枚举列表")
    public ResultCommon<List<HlEnum>> getEnum(String catalog, String type) {
        ResultCommon resultCommon = new ResultCommon();
        try {
            List<HlEnum> hlEnumList = hlEnumService.getEnumByCatalogAndType(catalog, type);
            resultCommon.setData(hlEnumList);
        } catch (Exception ex) {
            log.error("获取枚举列表失败", ex);
            resultCommon.setCode("500");
        }
        return resultCommon;
    }

    @PostMapping("/frontend/api/enum/save")
    @ApiOperation("枚举保存")
    public ResultCommon save(HlEnum enumDto) {
        ResultCommon resultCommon = new ResultCommon();
        try {
            hlEnumService.saveEnum(enumDto);
        } catch (Exception ex) {
            log.error("枚举保存失败", ex);
            resultCommon.setCode("500");
        }
        return resultCommon;
    }

    @GetMapping("/frontend/api/enum/{id}")
    @ApiOperation("根据ID获取枚举")
    public ResultCommon<HlEnum> getEnum(@PathVariable("id") int id) {
        ResultCommon<HlEnum> resultCommon = new ResultCommon();
        try {
            HlEnum byId = hlEnumService.getById(id);
            resultCommon.setData(byId);
        } catch (Exception ex) {
            log.error("获取枚举失败", ex);
            resultCommon.setCode("500");
        }
        return resultCommon;
    }

    @PostMapping("/frontend/api/enum/delete")
    @ApiOperation("根据ID删除枚举")
    public ResultCommon<HlEnum> deleteEnum(@RequestBody int[] id) {
        ResultCommon<HlEnum> resultCommon = new ResultCommon();
        try {
            hlEnumService.deleteEnum(id);
        } catch (Exception ex) {
            log.error("删除枚举失败", ex);
            resultCommon.setCode("500");
        }
        return resultCommon;
    }


}

